package com.example.administrator.mobilesafe.service;

import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;

import java.io.IOException;
import java.io.InputStream;

public class GPSService extends Service {
    private LocationManager locationManager;
    private MyLocationListener myLocationListener;
    public GPSService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        // List<String> provider = lm.getAllProviders();
        // for(String l: provider){
        // System.out.println(l);
        // }
        myLocationListener = new MyLocationListener();
        // 注册监听位置服务
        // 给位置提供者设置条件
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);

        // 设置参数细化：
        // criteria.setAccuracy(Criteria.ACCURACY_FINE);//设置为最大精度
        // criteria.setAltitudeRequired(false);//不要求海拔信息
        // criteria.setBearingRequired(false);//不要求方位信息
        // criteria.setCostAllowed(true);//是否允许付费
        // criteria.setPowerRequirement(Criteria.POWER_LOW);//对电量的要求

        String proveder = locationManager.getBestProvider(criteria, true);
        locationManager.requestLocationUpdates(proveder, 0, 0,myLocationListener);
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }


    class MyLocationListener implements LocationListener {

        /**
         * 当位置改变的时候回调
         */

        @Override
        public void onLocationChanged(Location location) {
            String longitude = "j:" + location.getLongitude() + "\n";
            String latitude = "w:" + location.getLatitude() + "\n";
            String accuracy = "a" + location.getAccuracy() + "\n";
            try {
                InputStream inputStream = getAssets().open("axisoffset.dat");
                ModifyOffset modifyOffset = ModifyOffset.getInstance(inputStream);
                PointDouble pointDouble = modifyOffset.s2c(new PointDouble(location.getLongitude(),location.getLatitude()));
                longitude ="j:" + modifyOffset.X+ "\n";
				latitude =  "w:" +modifyOffset.Y+ "\n";
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }

            SharedPreferences sp = getSharedPreferences("config", MODE_PRIVATE);
            SharedPreferences.Editor editor = sp.edit();
            editor.putString("lastlocation", longitude + latitude + accuracy);
            editor.commit();

        }
        /**
         * 当状态发生改变的时候回调 开启--关闭 ；关闭--开启
         */
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }

        /**
         * 某一个位置提供者可以使用了
         */
        @Override
        public void onProviderEnabled(String provider){
        }

        /**
         * 某一个位置提供者不可以使用了
         */
        @Override
        public void onProviderDisabled(String provider) {
        }

    }
    public void onDestroy() {
        super.onDestroy();
        // 取消监听位置服务
        locationManager.removeUpdates(myLocationListener);
        myLocationListener = null;
    }
}
